package com.mach.platform.service;

import com.mach.platform.domain.DevFollow;
import com.mach.platform.domain.iot.IotDev;
import com.mach.platform.repository.DevFollowRepo;
import com.mach.platform.utils.security.SecurityUtils;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @Description:
 * @Package: com.mach.platform.service
 * @time 2018/7/20 0020 11:03
 */
@Service
public class DevFollowServiceImpl implements BaseService<DevFollow> {

	@Autowired
	private DevFollowRepo devFollowRepo;

	/**
	 * 保存
	 *
	 * @param devFollow 保存的实体
	 * @return T 返回保存后的实体
	 */
	@Transactional
	@Override
	public DevFollow save (DevFollow devFollow) {

		DevFollow check = devFollowRepo.findByDeviceAndUser(devFollow.getDevice(), devFollow.getUser());

		if (check == null) {
			return devFollowRepo.saveAndFlush(devFollow);
		} else {
			return null;
		}
	}

	/**
	 * 插入，批量保存
	 *
	 * @param list 实体集合
	 * @return List<T>
	 */
	@Override
	public List<DevFollow> insertObjInBatch (List<DevFollow> list) {
		return null;
	}

	/**
	 * 更新
	 *
	 * @param devFollow 跟新的实体
	 * @return T
	 */
	@Override
	public DevFollow update (DevFollow devFollow) {
		return null;
	}

	/**
	 * 删除--批量删除
	 *
	 * @param ids 批量删除id
	 * @return int 影响数
	 */
	@Override
	public int delete (Long[] ids) {
		return 0;
	}

	/**
	 * 获取列表数据---使用 com.mach.platform.utils.springdata  下的DynamicSpecifications 的
	 * 写法进行分页查询
	 *
	 * @param pageable 分页参数
	 * @param params   其他参数
	 * @return Page<T>
	 */
	@Override
	public Page<DevFollow> list (Pageable pageable, Map params) {

		return null;
	}

	/**
	 * 获取单体 数据
	 *
	 * @param id 获取详情的id
	 * @return T
	 */
	@Override
	public DevFollow info (Long id) {
		System.out.println("ddddddddddddddddddddd:" + id);
		return devFollowRepo.getById(id);
	}

	/**
	 * 获取当前用户的关注 设备信息
	 *
	 * @return
	 */
	public List<IotDev> loadUserDevsFollowing ( ) {

		List<IotDev> result = null;

		Map params = new HashMap();
		params.put("user.id_eq", SecurityUtils.getCurrentUser().getId());
		Specification specification = DynamicSpecifications
				                              .bySearchFilter(SearchFilter.parse(params).values(), DevFollow.class);

		Page<DevFollow> devFollowRepoAll = devFollowRepo.findAll(specification, PageRequest.of(0, 100000));

		if (devFollowRepoAll.getContent().size() > 0) {

			result = devFollowRepoAll.getContent().stream().map(DevFollow::getDevice).map(item -> {
				item.setFollow(true);
				// todo  delete
				item.setStatus(1);
				return item;
			}).collect(Collectors.toList());

		} else {
			result = Collections.emptyList();
		}

		return result;
	}

	/**
	 * 关系 关注设备
	 *
	 * @param iotDev
	 */
	@Transactional(rollbackOn = Exception.class)
	public int deleteUserFollowData (IotDev iotDev) {

		return devFollowRepo.deleteByDeviceAndUser(iotDev, SecurityUtils.getCurrentUserByAccountDomain());
	}

}
